#!/bin/sh
set -x
################################################################################
# Title          : generateDocumentationAndDeploy.sh
# Date created   : 16Nov2018
# Original Author: "Jeroen de Bruijn"
# based on https://gist.github.com/vidavidorra/548ffbcdae99d752da02
#
# Preconditions:
# - Packages doxygen graphviz must be installed.
# - An gh-pages branch should already exist. See below for mor info on how to
#   create a gh-pages branch.
#
# Required global variables:
# - GH_DOC_TOKEN       : Secure token to the github repository.
#
# This script will generate Doxygen documentation and push the documentation to
# the gh-pages branch of a repository specified by $TRAVIS_REPO_SLUG
# Before this script is used there should already be a gh-pages branch in the
# repository.
#
# This file is processed by CMAKE to get the version in the commit message
# 
################################################################################

##### Setup this script and get the current gh-pages branch.
echo 'Setting up the script...'
GH_REPO_NAME=$(echo "$TRAVIS_REPO_SLUG" | awk -F/ '{print $2}')

# Exit with nonzero exit code if anything fails
set -e

# by the time this script is run, we should have already made the docs
cd "$TRAVIS_BUILD_DIR/build"
#docs should be in the $TRAVIS_BUILD_DIR/build/html directory
if [ ! -d "html" ] || [ ! -f "./html/v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@/@PROJECT_NAME@/index.html" ]; then
	echo '' >&2
	echo 'Warning: No documentation (html) files have been found!' >&2
	echo 'Warning: Not going to push the documentation to GitHub!' >&2
	exit 0
fi

if [ -z "${TRAVIS_TAG}" ] ; then
	echo 'Warning: Not a tag' >&2
	echo 'Warning: Not going to push the documentation to GitHub!' >&2
	exit 0
fi

# Get the current gh-pages branch
git clone -b gh-pages https://git@github.com/$TRAVIS_REPO_SLUG
cd "$GH_REPO_NAME"

# Remove everything currently in the gh-pages branch.
# GitHub is smart enough to know which files have changed and which files have
# stayed the same and will only update the changed files. So the gh-pages branch
# can be safely cleaned, and it is sure that everything pushed later is the new
# documentation.
rm -rf v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@/*

#copy the files over, this should add the new version
cp -a ../html/* ./

##### Configure git.
# Set the push default to simple i.e. push only the current branch.
git config --global push.default simple
# Pretend to be an user called Travis CI.
git config user.name "Autogenerated by Travis CI"
git config user.email "robin.getz@analog.com"

# Need to create a .nojekyll file to allow filenames starting with an underscore
# to be seen on the gh-pages site. Therefore creating an empty .nojekyll file.
# Presumably this is only needed when the SHORT_NAMES option in Doxygen is set
# to NO, which it is by default. So creating the file just in case.
if [ ! -f ".nojekyll" ] ; then
	touch .nojekyll
fi

################################################################################
##### Upload the documentation to the gh-pages branch of the repository.   #####

echo 'Uploading documentation to the gh-pages branch...'
# Add everything in this directory (the Doxygen code documentation) to the
# gh-pages branch.
#
# GitHub is smart enough to know which files have changed and which files have
# stayed the same and will only update the changed files.
git add --all

# Commit the added files with a title and description containing the Travis CI
# build number and the GitHub commit reference that issued this build.
git commit -m "Deploy autogenerated docs for ${GH_REPO_NAME} v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@-g@LIBIIO_VERSION_GIT@" --sign

# Force push to the remote gh-pages branch.
# The ouput is redirected to /dev/null to hide any sensitive credential data
# that might otherwise be exposed.
git push --force "https://${GH_DOC_TOKEN}@github.com/${TRAVIS_REPO_SLUG}" > /dev/null 2>&1

